import java.util.ArrayList;
import java.util.concurrent.TimeUnit;

public class Lib {

	ArrayList<String> list;

	private AbstractLock lock;

	boolean verbose = true;

	
	public Lib(AbstractLock lock, int size) {
		this.lock = lock;

		list = new ArrayList<String>(size);

		for (int i = 0; i < size; i++) {
			list.add(i, "startowy " + i);
		}

	}
	
	
	public String read(int i) {

		long time = System.nanoTime();
		long time1 = 1;
		String s = "";

		lock.readerIn();

		time = System.nanoTime() - time;

		s = list.get(i);

		if (verbose)
			System.out.println(Thread.currentThread().getName() + " reads: "
					+ s + " from: " + i);

		try {
			TimeUnit.MILLISECONDS.sleep(10);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		time1 = System.nanoTime();

		lock.readerout();

		time1 = System.nanoTime() - time1;

//		System.out.println("GET: " + time + " " + time1);
		
		StaticCouter.readCouter.add(time);

		return s;

	}

	public void put(int i, String s) {

		long time = System.nanoTime();

		lock.witerIn();

		time = System.nanoTime() - time;

		if (verbose)
			System.out.println(Thread.currentThread().getName() + " puts: " + s
					+ " at: " + i);

		list.add(i, s);

		try {
			TimeUnit.MILLISECONDS.sleep(10);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		long time1 = System.nanoTime();

		lock.writerout();

		time1 = System.nanoTime() - time1;

//		System.out.println("PUT: " + time + " " + time1);
		StaticCouter.wCouter.add(time);

	}



	public void setVerbose(boolean verbose) {
		this.verbose = verbose;
	}

}
